#include "Tank.h"
#include "Application.h"
#include <Math.h>
#include <algorithm>

void cat::Tank::think()
{
	double tvx = cos(_input.angle) * _input.velocity * 5.;
	double tvy = sin(_input.angle) * _input.velocity * 5.;

	_velocity.x += (tvx - _velocity.x) * 0.1;
	_velocity.y += (tvy - _velocity.y) * 0.1;

	_position.x += _velocity.x;
	_position.y += _velocity.y;

	_position.x = std::max(10., _position.x);
	_position.x = std::min(790., _position.x);

	_position.y = std::max(10., _position.y);
	_position.y = std::min(790., _position.y);

	_cannonY -= (_cannonY - _cannonTargetY) * 0.2;

	_visual.gunLeft.setOrigin(-7.f + (float)_cannonY, 12.f);
	_visual.gunRight.setOrigin(-7.f + (float)_cannonY, -8.f);

	if(_burstFrameTimer >=0)
	{
		if(_burstFrameTimer == 10)
		{
			FireLeft();
			FireRight();
			_cannonY = 10;
		}

		if(_burstFrameTimer == 5)
		{
			FireLeft();
			FireRight();
			_cannonY = 10;
		}

		if(_burstFrameTimer == 0)
		{
			FireLeft();
			FireRight();
			_cannonY = 10;
		}

		_burstFrameTimer--;
	}
}

void cat::Tank::draw()
{
	sf::RenderWindow& window = cat::Application::current().getWindow();

	_visual.turret.setPosition((float)_position.x, (float)_position.y);
	window.draw(_visual.turret);

	_visual.gunLeft.setPosition((float)_position.x,(float)_position.y);
	_visual.gunLeft.setRotation((float)_turretDirection * 180.f / 3.1415f);
	window.draw(_visual.gunLeft);

	_visual.gunRight.setPosition((float)_position.x, (float)_position.y);
	_visual.gunRight.setRotation((float)_turretDirection * 180.f / 3.1415f);
	window.draw(_visual.gunRight);
}

void cat::Tank::setVelocity(double velocity)
{
	_input.velocity = velocity;
}

void cat::Tank::move(double angle, double velocity)
{
	_input.angle = angle;
	_input.velocity = velocity;
}

void cat::Tank::turretTarget(double x, double y)
{
	_turretDirection = atan2(y - _position.y, x - _position.x);
}

void cat::Tank::update(double x, double y, double vx, double vy)
{
	_position.x = x;
	_position.y = y;
	_velocity.x = vx;
	_velocity.y = vy;

	_input.velocity = 0;
	_input.angle = 0;
}

void cat::Tank::FireLeft()
{
	cat::Application::current().getBulletFactory().create(
		_position.x + 11. * cos(_turretDirection) + 10. * sin(_turretDirection), 
		_position.y - 10. * cos(_turretDirection) + 11. * sin(_turretDirection),
		_turretDirection);
}

void cat::Tank::FireRight()
{
	cat::Application::current().getBulletFactory().create(
		_position.x + 11. * cos(_turretDirection) - 10. * sin(_turretDirection), 
		_position.y + 10. * cos(_turretDirection) + 11. * sin(_turretDirection), 
		_turretDirection);
}

void cat::Tank::fire()
{
	_burstFrameTimer = 15;
}


cat::Tank::Tank()
{
	_visual.turret.setRadius(10.f);
	_visual.turret.setOrigin(10.f, 10.f);
	_visual.turret.setFillColor(sf::Color(255u,255u,255u,30u));
	_visual.turret.setOutlineColor(sf::Color::Red);
	_visual.turret.setOutlineThickness(1.f);

	_visual.gunLeft.setSize(sf::Vector2f(10.f, 5.f));
	_visual.gunLeft.setFillColor(sf::Color::Red);
	_visual.gunLeft.setOutlineColor(sf::Color(0,0,0,100u));
	_visual.gunLeft.setOutlineThickness(1.f);

	_visual.gunRight.setSize(sf::Vector2f(10.f, 5.f));
	_visual.gunRight.setFillColor(sf::Color::Red);
	_visual.gunRight.setOutlineColor(sf::Color(0,0,0,100u));
	_visual.gunRight.setOutlineThickness(1.f);

	_position.x = 10.;
	_position.y = 10.;
	_velocity.x = 0.;
	_velocity.y = 0.;

	_input.angle = 0.;
	_input.velocity = 0.;

	_cannonTargetY = 0.;
	_cannonY = 0.;
}


cat::Tank::~Tank(void)
{
}
